package com.byit.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.byit.domain.AppApplication;
import com.byit.domain.AppApplictionMenu;
import com.byit.domain.AppKeywords;
import com.byit.domain.AppNews;
import com.byit.msg.MessageResult;
import com.byit.service.IAppKeywordsService;
import com.byit.solr.ISolrService;
import com.byit.solr.SolrResult;
import com.byit.utils.IpUtils;
import com.byit.utils.PageModel;
import com.byit.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查找
 *
 * @author tjm
 * @date 2019/1/11.
 */
@RestController
@RequestMapping("/app/search")
public class SearchController {

    @Autowired
    private ISolrService<AppApplication> appSolrService;
    @Autowired
    private ISolrService<AppApplictionMenu> menuSolrService;
    @Autowired
    private ISolrService<AppNews> newsSolrService;
    @Autowired
    private IAppKeywordsService keywordsService;
    @Autowired
    private SearchWordsAsync wordsAsync;

    @GetMapping("/select")
    public Object select(HttpServletRequest request,String title,Integer limitApp,Integer limitMenu,Integer limitNews){
        try {
            wordsAsync.addSearchNum(title);
            JSONObject param = new JSONObject();
            param.put("title",title);
            param.put("limit",limitApp);
            SolrResult appResult = appSolrService.select(param);
            param.put("limit",limitMenu);
            SolrResult menuResult = menuSolrService.select(param);
            param.put("limit",limitNews);
            SolrResult newResult = convertNews(newsSolrService.select(param),request);
            Map<String,Object> data = new HashMap<>();
            data.put("appResult",appResult);
            data.put("menuResult",menuResult);
            data.put("newResult",newResult);
            return MessageResult.success(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/selectApp")
    public Object selectApp(String title, Integer page, Integer limit){
        try {
            wordsAsync.addSearchNum(title);
            JSONObject param = new JSONObject();
            param.put("title",title);
            param.put("page",page);
            param.put("limit",limit);
            SolrResult solrResult = appSolrService.select(param);
            PageModel pageModel = new PageModel();
            pageModel.setLimit(solrResult.getSize());
            pageModel.setPage(solrResult.getCurrent().intValue());
            pageModel.setTotalPage(solrResult.getPages().intValue());
            return MessageResult.success(solrResult.getRecords(), pageModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/selectMenu")
    public Object selectMenu(String title, Integer page, Integer limit){
        try {
            wordsAsync.addSearchNum(title);
            JSONObject param = new JSONObject();
            param.put("title",title);
            param.put("page",page);
            param.put("limit",limit);
            SolrResult solrResult = menuSolrService.select(param);
            PageModel pageModel = new PageModel();
            pageModel.setLimit(solrResult.getSize());
            pageModel.setPage(solrResult.getCurrent().intValue());
            pageModel.setTotalPage(solrResult.getPages().intValue());
            return MessageResult.success(solrResult.getRecords(), pageModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/selectNews")
    public Object selectNews(HttpServletRequest request,String title, Integer page, Integer limit){
        try {
            wordsAsync.addSearchNum(title);
            JSONObject param = new JSONObject();
            param.put("title",title);
            param.put("page",page);
            param.put("limit",limit);
            SolrResult solrResult = convertNews(newsSolrService.select(param),request);
            PageModel pageModel = new PageModel();
            pageModel.setLimit(solrResult.getSize());
            pageModel.setPage(solrResult.getCurrent().intValue());
            pageModel.setTotalPage(solrResult.getPages().intValue());
            return MessageResult.success(solrResult.getRecords(), pageModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    private SolrResult convertNews(SolrResult solrResult,HttpServletRequest request){
        List<Map<String, Object>> records = solrResult.getRecords();
        String ipAddr = IpUtils.getRequestURL(request);
        records.forEach(o ->{
            if (o.containsKey("level") && "0".equals(o.get("level").toString())){
                o.put("label","置顶");
            }
            if (o.containsKey("picture")){
                List<String> list = StringUtil.splitStr(o.get("picture").toString(), ",");
                o.put("picture",list);
            }
            o.put("detailsUrl", ipAddr + "/app/news/" + o.get("id") + ".html");
        });
        return solrResult;
    }

    @GetMapping("/getKeywords")
    public Object getKeywords(String type, String seat){
        try {
            EntityWrapper<AppKeywords> wrapper = new EntityWrapper<>();
            wrapper.setSqlSelect("keyword");
            wrapper.eq("is_del",0);
            if (StringUtil.isNotEmpty(type)){
                wrapper.eq("type",type);
            }
            if (StringUtil.isNotEmpty(seat)){
                wrapper.eq("seat",seat);
            }
            wrapper.orderBy("by_order");
            wrapper.orderBy("update_time",false);
            wrapper.last("limit 12");
            List<Map<String, Object>> maps = keywordsService.selectMaps(wrapper);
            return MessageResult.success(maps);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
